package com.zhenyi.liuxue.config;

import com.cenjiawen.common.ArpHandler;
import com.cenjiawen.common.Config;
import com.jfinal.config.Constants;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.render.FreeMarkerRender;
import com.zhenyi.liuxue.core.LXRoutes;

import freemarker.template.Configuration;

public class LXConfig extends Config {

	@Override
	public void configConstant(Constants me) {
		super.configConstant(me);
		Configuration cfg = FreeMarkerRender.getConfiguration();
		cfg.setDefaultEncoding("utf-8");
	}

	@Override
	public void configRoute(Routes me) {
		if (PropKit.getBoolean("EN_LIUXUE", false)) {
			me.add(new LXRoutes());
		}
	}

	@Override
	protected void configSQLConnection(Plugins me) {
		// boolean debug = PropKit.getBoolean("devMode", false);
		if (PropKit.getBoolean("EN_LIUXUE", false)) {
			// addDataSource("zhenyi.asuscomm.com:9306", "liuxue", "root",
			// "zhenyi", me, new ArpHandler() {
			String host = PropKit.get("LX_DB", "192.168.50.222");
			String password = PropKit.get("LX_DB_PWD", "zhenyi");
			String user = PropKit.get("LX_DB_USER", "root");
			addDataSource(host, "liuxue", user, password, me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.liuxue.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("liuxue/liuxue.txt");
					arp.addSqlTemplate("liuxue/liuxueImport.txt");
					return arp;
				}
			});
		}
	}

	@Override
	public void configPlugin(Plugins me) {
		super.configPlugin(me);
	}

	@Override
	public void afterJFinalStart() {
		LogKit.info("Start Superlink config");
	}
}
